package com.example.videodisplay;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowInsetsController;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;

import com.example.videodisplay.constnt.MainConst;
import com.example.videodisplay.utils.SPUtils;

import java.util.concurrent.atomic.AtomicInteger;

import static android.view.View.SYSTEM_UI_FLAG_FULLSCREEN;
import static android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;


/**
 * @author xiaotie
 */
public class MainActivity extends AppCompatActivity {

    Context mContext;
    ViewFlipper vflp;
    //最小距离
    private final static int MIN_MOVE = 200;
    private GestureDetector mDetector;
    private Bitmap bitmap;
    ImageView[] points;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        if(SPUtils.getInstance(mContext).getBoolean(MainConst.SIGN)) {
            startActivity(new Intent(mContext, LoginActivity.class));
        }
        setWindows();
        setImageBitmapColor();
        initView();
    }

    private void initView() {
        View login = findViewById(R.id.to_login);
        login.setOnClickListener(v ->{
            SPUtils.getInstance(mContext).put(MainConst.SIGN,true);
            startActivity(new Intent(mContext, LoginActivity.class));
            finish();
        });
        vflp = (ViewFlipper) findViewById(R.id.view_flipper);
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
        points = new ImageView[3];
        //初始化布局中的小圆点ImageView控件
        for (int i = 0; i < points.length; i++) {
            //遍历LinearLayout下的所有ImageView子节点
            points[i] = (ImageView) linearLayout.getChildAt(i);
            //设置当前状态为允许点击（可点，灰色）
            points[i].setEnabled(true);
            //标识符与圆点顺序一致
            points[i].setTag(i);
        }
        points[0].setEnabled(false);
        //实例化SimpleOnGestureListener与GestureDetector对象
        MyGestureListener mgListener = new MyGestureListener();
        mDetector = new GestureDetector(this, mgListener);

    }

    /**
     * 更改图片颜色
     */
    private void setImageBitmapColor() {
        ImageView img=findViewById(R.id.vip_img);
        if (bitmap == null) {
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.vipmf);
        }
        Bitmap bm = createBitmapSafely(bitmap.getWidth(), bitmap.getHeight());
        Canvas canvas = new Canvas();
        canvas.setBitmap(bm);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);;
        PorterDuffColorFilter porterDuffColorFilter = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            porterDuffColorFilter = new PorterDuffColorFilter(Color.rgb(255,220,0), PorterDuff.Mode.SRC_ATOP);
        }
        p.setColorFilter(porterDuffColorFilter);
        canvas.drawBitmap(bitmap, 0, 0, p);
        p.setColorFilter(null);
        canvas.setBitmap(null);
        img.setImageBitmap(bm);
    }

    /**
     *创建一个图片 宽 高 色彩模式（4*8 =32位）
     */
    private static Bitmap createBitmapSafely(int width, int height) {
        try {
            return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        } catch (OutOfMemoryError error) {
            error.printStackTrace();
        }
        return null;
    }

    /**
     * 设置透明状态栏导航栏
     */

    private void setWindows(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            Window window = getWindow();
            window.setDecorFitsSystemWindows(false);
        }else {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            );
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }

    /**
     * 重写onTouchEvent触发MyGestureListener里的方法
     */
    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        return mDetector.onTouchEvent(event);
    }


    /**
     * 自定义一个GestureListener
     */
     class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        //抛掷（onFling）： 手指在触摸屏上迅速移动，并松开的动作。
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float v, float v1) {
            if(e1.getX() - e2.getX() > MIN_MOVE){
                if(vflp.getDisplayedChild()!=vflp.getChildCount()-1){
                vflp.setInAnimation(mContext,R.anim.right_in);
                vflp.setOutAnimation(mContext, R.anim.right_out);
                vflp.showNext();
                }
            }else if(e2.getX() - e1.getX() > MIN_MOVE){
                if(vflp.getDisplayedChild()!=0){
                    vflp.setInAnimation(mContext,R.anim.left_in);
                    vflp.setOutAnimation(mContext, R.anim.left_out);
                    vflp.showPrevious();
                }
            }
            vflp.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    for (ImageView point : points) {
                        point.setEnabled(true);
                    }
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    points[vflp.getDisplayedChild()].setEnabled(false);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            return true;
        }
    }

}